
ENT.Type = "anim"
ENT.Base = "base_gmodentity"

ENT.PrintName = "Printer"
ENT.Author = "[GU]|ZLC|thomasfn"
ENT.Contact = "Don't."
ENT.Instructions = "Spawn it, then press e to use it."
ENT.Purpose = ""

ENT.Spawnable = false
ENT.AdminSpawnable = false

ENT.ItemModel = "models/props_lab/plotter.mdl"
util.PrecacheModel( ENT.ItemModel )

ENT.ItemSound = "npc/dog/dog_servo12.wav"
util.PrecacheSound( ENT.ItemSound )

ENT.IsPrinter = true

if (SERVER) then

	AddCSLuaFile( "shared.lua" )
	
	ENT.NextUse = CurTime()
	ENT.PrintTimer = 7

	function ENT:Initialize()
		self.Entity:SetModel(self.ItemModel)
		self.Entity:PhysicsInit(SOLID_VPHYSICS)
		self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
		self.Entity:SetSolid(SOLID_VPHYSICS)
		
		if (!self.Data) then self.Data = {} end
		if (!self.Data[self:EntIndex()]) then self.Data[self:EntIndex()] = {} end

		local phys = self.Entity:GetPhysicsObject()
		if (phys && phys:IsValid()) then phys:Wake() end
	end
	
	function ENT:Use( activator, ply )
		if (CurTime() < self.NextUse) then return end
		self.NextUse = CurTime()+1
	end
	
	function ENT:PrintDocument( text )
		if (self.Data[ self:EntIndex() ].Printing) then return end
		self.Data[ self:EntIndex() ].Printing = true
		self.Entity:EmitSound( self.ItemSound )
		PCBeam:SetValue( self, "Printing", true )
		timer.Create( "PRINTER_" .. tostring( self:EntIndex() ) .. "_PRINT", self.PrintTimer, 1, function( self, text )
			self.Entity:StopSound( self.ItemSound )
			PCBeam:SetValue( self, "Printing", false )
			self.Data[ self:EntIndex() ].Printing = false
			local pos = self.Entity:GetPos() + Vector( 0, 0, 45 )
			local ent = ents.Create( "pcmod_letter" )
			ent:SetPos( pos )
			ent:Spawn()
			// ent:SetOwner( self:GetOwner() )
			ent.Text = text
		end, self, text )
		timer.Create( "PRINTER_" .. tostring( self:EntIndex() ) .. "_NOISE", 0.75, (self.PrintTimer/0.75)-1, function( self )
			self.Entity:EmitSound( self.ItemSound )
		end, self )
	end
	
end

if (CLIENT) then

	function ENT:Draw()
		// Draw our entity
		self.BaseClass.Draw( self )

	end
	
end